import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@renderer/views/login/Login.vue'
import Home from '@renderer/views/home/Home.vue'
import routeConstants from '@renderer/routers/routeConstants'
import Patients from '@renderer/views/patients/Patients.vue'
import SystemSetting from '@renderer/views/serviceMode/SystemSetting/SystemSetting.vue'
import ServiceLayout from '@renderer/views/serviceMode/ServiceLayout.vue'
import ServiceChoose from '@renderer/views/serviceMode/ServiceChoose/ServiceChoose.vue'
import { RouteMode } from '@renderer/types'
import MultiScanSeriesPage from '@renderer/views/multiScanSeriesPage/MultiScanSeriesPage.vue'
import Views from '@renderer/views/views/Views.vue'
import { useScanStore } from '@renderer/store/pinia/scan'
import { useSystemSettingStore } from '@renderer/store/pinia/systemSetting'
import { IS_OPEN_AUTHORITY } from '@renderer/config'
import { GlobalNotification } from '@renderer/components/Toast/GlobalNotification'
import ShutdownPage from '@renderer/views/shutdown/ShutdownPage.vue'
import DicomPrint from '@renderer/views/service/dicomPrint/DicomPrint.vue'
import MprPage from '@renderer/views/imagePage/MprPage.vue'

const Layout = () => import( '@renderer/layout/Layout.vue')
const routes = [
  {
    path: '/',
    name: 'root',
    component: Layout,
    children: [
      {
        path: routeConstants.HomePage.path,
        name: routeConstants.HomePage.name,
        component: Home,
        meta: {
          requireAuth: true,
          mode: RouteMode.default
        }
      },
      {
        path: routeConstants.LoginPage.path,
        name: routeConstants.LoginPage.name,
        component: Login,
        meta: {
          requireAuth: false,
          mode: RouteMode.default
        }
      },
      {
        path: routeConstants.PatientsPage.path,
        name: routeConstants.PatientsPage.name,
        component: Patients,
        meta: {
          requireAuth: true,
          mode: RouteMode.patients
        }
      },
      {
        path: routeConstants.ViewPage.path,
        name: routeConstants.ViewPage.name,
        component: Views
      },
      {
        path: routeConstants.ShutdownPage.path,
        name: routeConstants.ShutdownPage.name,
        component: ShutdownPage
      }
    ]
  },
  {
    path: '/scan',
    component: Layout,
    children: [
      {
        path: routeConstants.MultiScanSeriesPage.path,
        name: routeConstants.MultiScanSeriesPage.name,
        component: MultiScanSeriesPage,
        meta: {
          keepAlive: true,
          requireAuth: true,
          mode: RouteMode.normalScan
        }
      },
      {
        path: routeConstants.MprPage.path,
        name: routeConstants.MprPage.name,
        component: MprPage,
        meta: {
          keepAlive: true,
          requireAuth: true,
          mode: RouteMode.normalScan
        }
      }
    ]
  },
  {
    path: '/s',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'serviceLayout',
        component: ServiceLayout,
        children: [
          {
            path: routeConstants.SERVICE_CHOOSE.path,
            name: routeConstants.SERVICE_CHOOSE.name,
            component: ServiceChoose,
            meta: {
              requireAuth: true,
              mode: RouteMode.service
            }
          },
          {
            path: routeConstants.SERVICE_SYSTEM_SETTING.path,
            name: routeConstants.SERVICE_SYSTEM_SETTING.name,
            component: SystemSetting,
            meta: {
              requireAuth: true,
              mode: RouteMode.service
            }
          }
        ]
      }
    ]
  },
  {
    path: '/helper',
    component: Layout,
    children: [
      {
        path: routeConstants.HELP_DICOM_PRINT.path,
        name: routeConstants.HELP_DICOM_PRINT.name,
        component: DicomPrint
      }
    ]
  }
]
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes: routes// `routes: routes` 的缩写
})

function setKeepAlive(to: any) {
  if (to.meta?.keepAlive) {
    const scanStore = useScanStore()
    scanStore.setKeepAliveRouteArr(to.name)
  }
}

async function checkAuth(to, next) {
  const systemSetting = useSystemSettingStore()
  if (to.meta.requireAuth && IS_OPEN_AUTHORITY && systemSetting.access_token !== '') {
    GlobalNotification({
      title: 'Error',
      message: '登录失效,请重新登录',
      type: 'error'
    })
    next(false)
    await router.push(routeConstants.LoginPage.path)
  } else {
    next()
  }
}

router.beforeEach(async (to, _, next) => {
  console.log(`to: ${to.path}`)
  // checkAuth(to, next)
  setKeepAlive(to)
  next()
})

export {
  router,
  routes
}
